<template>
  <div class="app-container">
    <div class="menu-container">
      <el-form :inline="true" style="float: left;">
        <el-button size="mini" type="primary" @click="createVideo()">添加视频</el-button>
        <!-- <el-button size="mini" type="primary" @click="batchPutOnShelves">批量上架</el-button>
        <el-button size="mini" type="warning" @click="batchPullOffShelves">批量下架</el-button> -->
      </el-form>
      <div style="clear:both"/>
    </div>
    <el-table v-loading="listLoading" :data="page.records" border fit highlight-current-row style="width: 100%" height="500">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="名称">
        <template slot-scope="scope">
          <span>{{ scope.row.title }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="所属主题">
        <template slot-scope="scope">
          <span>{{ scope.row.unit }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" label="销量">
        <template slot-scope="scope">
          <span>{{ scope.row.sold_num }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="Actions" width="250">
        <template slot-scope="scope">
          <el-button type="primary" size="small" icon="el-icon-edit" @click="modifyVideo(scope.row)">修改</el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="delVideo(scope.row)">删除</el-button>
          <!-- <el-button type="primary" size="small" @click="managerCategoryOptions(scope.row)">视频选项管理</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="page.recordsTotal>0" :total="page.recordsTotal" :page.sync="listQuery.pageNum" :page-sizes="[20, 50, 100]" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <el-dialog :visible.sync="dialogFormVisible" :title="dialogTitle" log- width="80%">
      <video-detail :is-edit="isEdit" :current-id="currentVideoId"/>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList, deleteVideo } from '@/api/video'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import VideoDetail from './components/VideoDetail'

export default {
  name: 'VideoList',
  components: { Pagination, VideoDetail },
  data() {
    return {
      dialogTitle: '新增视频',
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 100
      },
      isEdit: false,
      currentVideoId: undefined,
      formLabelWidth: '100px',
      dialogFormVisible: false,
      stateOptions: [{ value: 0, label: '正常' }, { value: 1, label: '禁用' }],
      categoryForm: { 'id': undefined, 'name': '', 'show_order': 1, 'state': 0 },
      page: { pageNum: 1, pageSize: 100, pageTotal: 0, recordsTotal: 0, records: [] }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    batchPutOnShelves() {
      var desc = '是否将所有选中视频上架'
      var params = { 'state': 0 }
      this._doSetBase(params, desc)
    },
    batchPullOffShelves() {
      var desc = '是否将所有选中视频下架'
      var params = { 'state': 1 }
      this._doSetBase(params, desc)
    },
    _doSetBase(params, desc, cancelMsg) {
      if (!this.selectedVideos || this.selectedVideos.length <= 0) {
        this.$message({
          message: '至少选择一个视频',
          type: 'error'
        })
        return
      }
      params['videoIds'] = this._getVideoIds(this.selectedVideos)
      if (!desc) {
        desc = '批量更新视频状态, 是否继续?'
      }
      if (!cancelMsg) {
        cancelMsg = '已取消操作'
      }
      this.myConfirm(desc, this._updateVideo, params, cancelMsg)
    },
    _updateVideo(params) {
      this.axios.post('/api/video/batch-update', params).then(response => {
        const r = response.data
        if (r.code !== 0) {
          this.$message({
            message: r.msg,
            type: 'error'
          })
          return
        }

        this.selectedGoods = []
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.list()
      })
    },
    createVideo() {
      this.isEdit = false
      this.currentVideoId = undefined
      this.dialogTitle = '添加视频'
      this.dialogFormVisible = true
    },
    modifyVideo(video) {
      this.isEdit = true
      this.currentVideoId = video.id
      this.dialogTitle = '编辑视频'
      this.dialogFormVisible = true
    },
    delVideo(video) {
      this.myConfirm('即将删除视频, 是否继续?', this._delVideo, video, '已取消操作')
    },
    _delVideo(video) {
      deleteVideo(video).then(response => {
        const r = response.data
        if (r.code === 0) {
          this.$message({ message: '操作成功', type: 'success' })
          this.getList()
        } else {
          this.$message({ message: r.msg, type: 'error' })
        }
      })
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        const data = response.data
        if (data.code !== 0) {
          this.$message.error(data.msg)
        }
        this.page = data.data
        this.listLoading = false
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
